package com.example.aiquestions.service.impl;

import com.example.aiquestions.entity.Subject;
import com.example.aiquestions.mapper.SubjectMapper;
import com.example.aiquestions.service.SubjectService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class SubjectServiceImpl implements SubjectService {

    @Autowired
    private SubjectMapper subjectMapper;

    @Override
    public PageInfo<Subject> getSubjectsByUserId(Long userId, int pageNum, int pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        List<Subject> list = subjectMapper.getSubjectsByUserId(userId);
        return new PageInfo<>(list);
    }

    @Override
    @Transactional
    public Subject createSubject(Long userId, String name, String description) {
        Subject subject = new Subject();
        subject.setName(name);
        subject.setDescription(description);
        subject.setCreatedId(userId);
        subject.setCreatedAt(new java.util.Date());
        subject.setUpdatedAt(new java.util.Date());
        subject.setStatus(1);
        
        subjectMapper.createSubject(subject);
        return subject;
    }

    @Override
    @Transactional
    public Subject updateSubject(Long id, String name, String description) {
        Subject subject = getSubjectById(id);
        subject.setName(name);
        subject.setDescription(description);
        subject.setUpdatedAt(new java.util.Date());
        
        subjectMapper.updateSubject(subject);
        return subject;
    }

    @Override
    @Transactional
    public boolean deleteSubject(Long id) {
        return subjectMapper.deleteSubject(id) > 0;
    }

    @Override
    public Subject getSubjectById(Long id) {
        return subjectMapper.getSubjectById(id);
    }
} 